<template>
    <el-dialog title="编辑"
               :close-on-click-modal="false"
               :visible.sync="visible"
               @close="close">
      <el-form ref="form" :model="dataForm" :rules="dataRule" label-width="120px">
        <el-form-item label="设备类型名称：" prop="name">
          <el-input v-model="dataForm.name" placeholder="请输入设备类型名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="设备状态：">
          <el-switch
            v-model="dataForm.enableFlagBool"
            active-text="启用"
            inactive-text="禁用">
          </el-switch>
        </el-form-item>
        <el-form-item>
          <el-button @click.stop="visible=false">取消</el-button>
          <el-button type="primary" :loading="loading" @click.stop="confirm">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
</template>

<script>
  export default {
    name: 'updateDetail',
    data () {
      return {
        visible: false,
        loading:false,
        dataForm: {
          id: 0,
          name:'',
          enableFlag: 1,
          enableFlagBool:false,
        },
        dataRule:{
          name:[{ required: true, message: '设备类型名称不能为空', trigger: 'blur' }]
        }
      }
    },
    methods: {
      init (model) {
        this.visible = true
        this.loading=false
        let obj = JSON.parse(JSON.stringify(model))
        this.dataForm.enableFlag = obj.enableFlag
        this.dataForm.id = obj.id
        this.dataForm.name = obj.name
        if(obj.enableFlag==1){
            this.dataForm.enableFlagBool = true
        }
      },
      close () {
        this.visible = false
      },
      confirm(){
        this.$refs['form'].validate((valid)=>{
          if (valid){
            this.loading=true
            this.dataForm.enableFlag=0
            if(this.dataForm.enableFlagBool){
              this.dataForm.enableFlag=1
            }
            console.log(this.dataForm)
            this.$http({
              url:this.$http.adornUrl('/v1/charging/type/merchant/update'),
              method:'POST',
              data:this.$http.adornData(this.dataForm)
            }).then(({data})=>{
              this.loading=false
              this.$message({
                type:'success',
                message:'更新成功',
                duration:1500,
                onClose:()=>{
                  this.visible=false
                  this.$emit('refreshDataList')
                }
              })
            }).catch((error)=>{
              this.loading=false
            })
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
